package com.zhw.goshopping.sso.controller;

import com.zhw.goshopping.common.pojo.GoShoppingResult;
import com.zhw.goshopping.common.utils.CookieUtils;
import com.zhw.goshopping.pojo.TbUser;
import com.zhw.goshopping.sso.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Create by zhaihongwei on 2018/5/2
 * 用户处理Controller
 */
@Controller
public class UserController {
    @Value("${TOKEN_KEY}")
    private String TOKEN_KEY;

    @Autowired
    private UserService userService;

    @RequestMapping("/user/check/{param}/{type}")
    @ResponseBody
    public GoShoppingResult checkUserData(@PathVariable String param, @PathVariable Integer type) {
        GoShoppingResult result = userService.checkData(param, type);
        return result;
    }

    @RequestMapping(value="/user/register", method=RequestMethod.POST)
    public String register(TbUser user) {
        GoShoppingResult result = userService.register(user);
        if (result.getStatus()==200) {
            return "login";
        }
        return null;
    }

    @RequestMapping(value="/user/login", method= RequestMethod.POST)
    @ResponseBody
    public GoShoppingResult login(String username, String password,
                              HttpServletResponse response, HttpServletRequest request) {
        GoShoppingResult result = userService.login(username, password);
        //登录成功后写cookie
        if (result.getStatus() == 200) {
            //把token写入cookie
            CookieUtils.setCookie(request, response, TOKEN_KEY, result.getData().toString());
        }
        return result;
    }

    @RequestMapping(value="/user/logout/{token}", method= RequestMethod.GET)
    @ResponseBody
    public GoShoppingResult logout(@PathVariable String token,HttpServletResponse response, HttpServletRequest request) {
        GoShoppingResult result = userService.logout(token);
        if (result.getStatus() == 200) {
            // 退出之后删除cookie中的token
            CookieUtils.deleteCookie(request, response, TOKEN_KEY);
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return result;
    }

    //jsonp的第二种方法，spring4.1以上版本使用
    @RequestMapping(value="/user/token/{token}", method=RequestMethod.GET)
    @ResponseBody
    public Object getUserByToken(@PathVariable String token, String callback) {
        GoShoppingResult result = userService.getUserByToken(token);
        //判断是否为jsonp请求
        if (StringUtils.isNotBlank(callback)) {
            MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result);
            //设置回调方法
            mappingJacksonValue.setJsonpFunction(callback);
            return mappingJacksonValue;
        }
        return result;
    }
}
